package com.wangzaiplus.test.juc.thread.CountDownLatch;

import org.junit.runner.notification.RunListener;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName CountDownLatchDemo
 * @Description: TODO
 * @Author wgc
 * @Date 2020/06/29 11:26
 * @Version V1.0
 **/
public class CountDownLatchDemo {

     public  static  class  T1 extends  Thread{
         int sleepSeconds;//休眠多少秒
         CountDownLatch countDownLatch;
         public  T1(String name,int sleepSeconds,CountDownLatch countDownLatch){
             super(name);
             this.sleepSeconds=sleepSeconds;
             this.countDownLatch=countDownLatch;
         }
         @Override
         public void run() {
             Thread thread = Thread.currentThread();
             long startTime=System.currentTimeMillis();
             System.out.println(startTime+" ,"+thread.getName()+" 开始处理");
             try {
                 TimeUnit.SECONDS.sleep(this.sleepSeconds);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }finally {
                 countDownLatch.countDown();
             }
             long endTime=System.currentTimeMillis();
             System.out.println(endTime+","+thread.getName()+",处理完毕，耗时"+(endTime-startTime));
         }
     }

     public static void main(String[] args) throws InterruptedException {
         System.out.println(System.currentTimeMillis()+","+Thread.currentThread().getName()+"线程 start");
          CountDownLatch countDownLatch=new CountDownLatch(2);
         long startTime=System.currentTimeMillis();
         T1 t1=new T1("T1",2,countDownLatch);
         t1.start();

         T1 t2=new T1("T2",5,countDownLatch);
         t2.start();
         countDownLatch.await();

         System.out.println(System.currentTimeMillis()+" , "+Thread.currentThread().getName()+"线程 end");

         long endTime=System.currentTimeMillis();

         System.out.println("总耗时:"+(endTime-startTime));
     }
}
